﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;

using Survey.Common;
using Survey.Common.Business.Services;
using Survey.Common.Controllers.Services;
using Survey.Common.Models.Services;
using Survey.Entity.Services;

namespace Survey.Mvc.Controllers.Services
{
    public class TranslatorController
        : EntityController<Translator, TranslatorFilter, ITranslatorModel, ITranslatorFacade>, ITranslatorController
    {
        #region abstract ClearFilter
        /// <summary>
        ///     Pre-sets filtering filters
        /// </summary>
        protected override TranslatorFilter ClearFilter()
        {
            var filter = base.ClearFilter();
            filter.SearchedLanguageName = CultureInfo.CurrentCulture.Name;
            filter.OrderBy["Key"] = true;

            return filter;
        }
        #endregion abstract ClearFilter

        #region OnList
        /// <summary>
        ///     Builds the list of translations,
        ///     containing columns dependent on Application Languages
        /// </summary>
        protected override IEnumerable<IItemToDisplay> LoadItemsToDisplay(TranslatorFilter filter)
        {
            var itemsToDisplay = new List<IItemToDisplay>();
            foreach (var i in Facade.Find(filter))
            {
                IItemToDisplay itd = new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.ToDisplay(),
                    Items = new List<ICellDescription>
                    {
                        i.Cell(() => i.Key),
                        i.Cell(() => i.Area),
                    }
                };

                foreach (var lang in LanguageFacade.GetAll())
                {
                    itd.Items.Add(new CellDescription
                    {
                        HeaderName = lang.NativeName,
                        Value = i.Translations.ContainsKey(lang.LanguageName)
                                    ? i.Translations[lang.LanguageName]
                                    : string.Empty,
                        Sort = (filter.SearchedPhrase.IsEmpty()
                               || lang.LanguageName == filter.SearchedLanguageName),
                        Localize = false,
                        SortProperty = "Phrases." + lang.LanguageName,
                    });
                }
                itd.Items.Add(i.Cell(() => i.ID, new CellDescription { Align = Align.right, Format = "N0" }));

                itemsToDisplay.Add(itd);
            }

            return itemsToDisplay;
        }

        /// <summary>
        ///     Provides columns for Excel
        /// </summary>
        protected override IEnumerable<IEnumerable<IExCell>> LoadExcelItemsToDisplay(TranslatorFilter filter)
        {
            IList<IList<IExCell>> itemsToExcel = new List<IList<IExCell>>();
            foreach (var i in Facade.Find(Model.Filter))
            {
                var row = new List<IExCell>
                {
                    i.ExCell(() => i.ID),
                    i.ExCell(() => i.Key),
                    i.ExCell(() => i.Area),
                };

                foreach (var lang in LanguageFacade.GetAll())
                {
                    row.Add(new ExCell
                    {
                        Name = lang.LanguageName,
                        Localized = lang.NativeName,
                        Value = i.Translations.ContainsKey(lang.LanguageName)
                                    ? i.Translations[lang.LanguageName]
                                    : string.Empty,
                        Type = ExCellType.String,
                    });
                }

                itemsToExcel.Add(row);
            }

            return itemsToExcel;
        }
        #endregion OnList

        #region OnBefore
        protected override void OnBeforeFind()
        {
            base.OnBeforeFind();
            Model.LanguagesModel.ComboBoxName = "SelectedLanguage";
            Model.LanguagesModel.ShowEmpty = false;
            var list = LanguageFacade.GetAll();
            var currentLanguage = Model.Filter.SearchedLanguageName;
            if (!list.Any(l => l.LanguageName.IsEqual(currentLanguage)))
            {
                var lang =
                    list.Where(l => currentLanguage.StartsWith(l.LanguageName, StringComparison.OrdinalIgnoreCase)).
                        FirstOrDefault();
                if (lang.Is())
                {
                    currentLanguage = lang.LanguageName;
                }
            }
            Model.LanguagesModel.Items = new SelectList(list, "LanguageName", "NativeName", currentLanguage);
        }
        #endregion OnBefore

        #region properties
        public override string ControllerName { get { return Str.Controllers.Translator; } }

        public virtual ILanguageFacade LanguageFacade { protected get; set; }
        #endregion properties
    }
}